package frame.exec 
{
	import common.StructGroup;
	import common.StructStaff;
	import common.StructTime;
	import config.COLORCONFIG;
	import config.FRAME_EXEC;
	import data.DataMonitor;
	import flash.display.Bitmap;
	import flash.geom.Point;
	import flash.text.TextFormatAlign;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	import frame.IFrame;
	import mx.utils.StringUtil;
	import nsdecl.ns_drag_slot;
	import nsdecl.ns_drag_type;
	import nsdecl.ns_group_handler;
	import nsdecl.ns_group_hook;
	import nsdecl.ns_interactive_slot;
	import nsdecl.ns_vname_decl;
	import proxy.DataAccess;
	import ui.basePannel;
	import ui.core.res_access;
	import ui.ui_button;
	import ui.ui_icon_button;
	import ui.ui_scroll_bar;
	import ui.ui_text;
	import ui.uiGroup;
	import ui.uiUnit;
	import ui.uiWin;
	import ui.utils.initUIContainer;
	import vndecl.*;
	/**
	 * ...
	 * @author anod
	 */
	public class SetGroupAssignment extends basePannel implements IFrame
	{
		use namespace ns_vname_decl;
		use namespace ns_drag_type;
		private static const MEMBER_LINE_MAX:int = 480;
		
		private var mFrame:FSetExecGroup;
		
		public function SetGroupAssignment( frame:FSetExecGroup ) 
		{
			mFrame = frame;
			addChild( mFrame );
			
			super();
			setupInteractive();
			setupDataMonitor();
		}
		
		/* INTERFACE frame.IFrame */
		
		public function setupInteractive():void 
		{
			iNextButton.click   = "nextPanel";
			iPrevButton.click   = "prevPanel";
			iCancelButton.click = "switchChild";
			iGroupTable.handler = "listGroupAssignment";
			iGroupTable.hook    = "scrollable";
			iScrollBar.area     = "iGroupTable";
			iScrollBar.scroll   = "scrollPlaceSprite";
		}
		
		public function setupDataMonitor():void 
		{
			iGroupTable.data = VNGM.PROXY_GROUP;
		}
		
		// UI回调
		//----------------------------------------------
		ns_group_handler function listGroupAssignment( entry:uiUnit, value:StructGroup ):void
		{
			var content:UGroupOption = new UGroupOption();
			
			content.iNameText.component.text = "分组：" + value.name;
			content.iTimesegBox.component.data = makeTimeArray();
			content.iMemberList.component.data = entry.owner.data + "." + entry.key + "." + "members";
			content.iMemberList.component.handler = "listGroupMembers";
			content.iMemberList.component.hook = "resetMembersPos";
			
			entry.addChild( content );
		}
		ns_group_handler function listGroupMembers( entry:uiUnit, member_id:int ):void
		{
			var staff:StructStaff = DataAccess.instance.queryStaffInterf().getByID( member_id ) as StructStaff;
			
			var txt:ui_text = new ui_text();
			txt.size = 14;
			txt.color = COLORCONFIG.TEXT_WHITE;
			txt.text = StringUtil.substitute("{0} {1}@{2}", entry.key + 1, staff.name, staff.identity );
			txt.show_display();
			txt.margin = txt.blank = 2;
			txt.width = txt.textfield.width + 5;
			txt.height = txt.textfield.height + 5;
			txt.background = "img0299";
			txt.align = TextFormatAlign.CENTER;
			
			entry.addChild( txt );
			entry.cacheAsBitmap = true;
			
			entry.dragAccept = entry.dragType = FRAME_EXEC.MEMBER_ADJUST_ORDER( getGroupByUnit(entry).ID );
			entry.dragDrop = "adjustOrder";
			entry.dragIn = "markDropPos";
			entry.dragOut = entry.dragClean = "clearDropPos";
			entry.over = "useDragCursor";
			entry.out = "useArrowCursor";
			entry.name = String( entry.key );
		}
		ns_group_hook function scrollable( group:uiGroup ):void
		{
			var loop_n:int = group.numChildren;
			var y_ctrl:int = 0;
			for ( var i:int = 0; i < loop_n; ++i )
			{
				var entry:uiUnit = group.getChildAt(i) as uiUnit;
				entry.y = y_ctrl;
				y_ctrl += entry.height + 4;
			}
			iScrollBar.visible = group.height > mFrame.iGroupTable.scrollRect.height;
		}
		ns_group_hook function resetMembersPos( group:uiGroup ):void
		{
			var mMemberLineControl:Point = new Point();
			var loop_n:int = group.numChildren;
			for ( var i:int = 0; i < group.numChildren; ++i )
			{
				var entry:uiUnit = group.getChildByName( i.toString() ) as uiUnit;
				var txt:ui_text = entry.getChildAt(0) as ui_text;
				var text:String = txt.text;
				var pos:int = text.indexOf(" ");
				txt.text = String(i+1) + text.slice(pos);
				
				entry.x = mMemberLineControl.x > 0 ? mMemberLineControl.x : 0;
				entry.y = mMemberLineControl.y;
				
				mMemberLineControl.x += txt.textfield.width + 6;
				if ( mMemberLineControl.x > MEMBER_LINE_MAX )
				{
					mMemberLineControl.x = 0;
					mMemberLineControl.y += 24;
				}
			}
		}
		ns_drag_slot function adjustOrder( from:uiUnit, to:uiUnit ):void
		{
			// 确定插入位置
			var pos:Point = new Point(stage.mouseX, stage.mouseY);
			pos = to.globalToLocal( pos );
			var from_index:int = int( from.name );
			var to_index:int = int( to.name );
			if ( pos.x < to.width / 2 ) 
				(from_index < to_index) && (to_index = to_index - 1);
			else ( from_index > to_index ) && (to_index = to_index + 1);
			
			if ( to_index == from_index ) return;				//无效操作
			
			// 设置, from,to之间的id都要增加1
			var group:uiGroup = to.owner;
			var loop_n:int = group.numChildren;
			var min:int = Math.min(from_index, to_index);
			var max:int = Math.max(from_index, to_index);
			for ( var i:int = 0; i < loop_n; ++i )
			{
				var entry:uiUnit = group.getChildAt(i) as uiUnit;
				var index:int = int( entry.name );
				if ( min <= index && index <= max )
				{
					entry.name = from_index > to_index ?  String( index + 1 ) : String( index - 1 );
				}
			}// end of for
			from.name = to_index < loop_n ? String( to_index ) : String( loop_n-1 );
			ns_group_hook::resetMembersPos( group );
		}
		ns_drag_slot function markDropPos( entry:uiUnit ):void
		{
			//检查到底是左边还是右边
			var pos:Point = new Point(stage.mouseX, stage.mouseY);
			pos = entry.globalToLocal( pos );
			if ( pos.x < entry.width/2 ) pos = new Point( 0, entry.getChildAt(0).height);
			else pos = new Point(entry.getChildAt(0).width+1, entry.getChildAt(0).height);
			
			with ( entry.graphics )
			{
				clear();
				lineStyle(2, COLORCONFIG.Q_ORANGE, 1)
				moveTo(pos.x, 0);
				lineTo(pos.x, pos.y);
			}
		}
		ns_drag_slot function clearDropPos( entry:uiUnit ):void
		{
			entry.graphics.clear();
		}
		ns_interactive_slot function useDragCursor( entry:uiUnit ):void
		{
			Mouse.cursor = MouseCursor.HAND;
		}
		ns_interactive_slot function useArrowCursor( entry:uiUnit ):void
		{
			Mouse.cursor = MouseCursor.AUTO;
		}
		
		//常用函数
		//------------------------------------
		private function makeTimeArray():Array
		{
			var timesegs:Vector.<StructTime> = DataMonitor.getData( VNGM.PROXY_TIMESEG );
			var retval:Array = [];
			retval.push( "--不安排--" );
			for each( var timeseg:StructTime in timesegs )
				retval.push( timeseg.name );
			return retval;
		}
		private function getGroupByUnit( entry:uiUnit ):StructGroup
		{
			var member_group:uiGroup = entry.owner;
			var unit_group:uiUnit = member_group.parent.parent.parent as uiUnit;
			return unit_group.data;
		}
		
		//交互组件定义
		//------------------------------------
		public function get iNextButton():ui_button
		{
			return mFrame.iNextButton.component;
		}
		public function get iPrevButton():ui_button
		{
			return mFrame.iPrevButton.component;
		}
		public function get iCancelButton():ui_button
		{
			return mFrame.iCancelButton.component;
		}
		public function get iScrollBar():ui_scroll_bar
		{
			return mFrame.iScrollBar.component;
		}
		public function get iGroupTable():uiGroup
		{
			return mFrame.iGroupTable.component;
		}
	}

}